package com.bkgy.modular.yieldReport.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.yieldReport.dto.AosrpeDto;
import com.bkgy.modular.yieldReport.dto.TbDsjActWeightDto;
import com.bkgy.modular.yieldReport.entity.ExportTableDataPP;
import com.bkgy.modular.yieldReport.entity.ExporttableData;
import com.bkgy.modular.yieldReport.entity.TableData;
import com.bkgy.modular.yieldReport.entity.TableDataPP;
import com.bkgy.modular.yieldReport.service.ReportFormsService;
import com.bkgy.modular.yieldReport.service.YieldReportService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 1.0
 * @ClassName TbDsjActWeightController
 * @date 2023/3/31 16:27
 */
@RestController
@RequestMapping("/yieldReport")
public class TbDsjActWeightController {

    @Resource
    private YieldReportService yieldReportService;

    @Resource
    private ReportFormsService reportFormsService;

    @PostMapping("/findByData")
    public ResultData findByData(@RequestBody TbDsjActWeightDto dto) {
        return yieldReportService.findByData(dto);
    }

    @PostMapping("/reportFormsFindByData")
    public ResultData reportFormsFindByData(@RequestBody TbDsjActWeightDto dto) {
        return reportFormsService.reportFormsFindByData(dto);
    }

    @PostMapping("/reportFormsFinDataSXSD")
    public ResultData reportFormsFindDataSXSD(@RequestBody JSONObject jsonObject) {
        return reportFormsService.reportFormsFindDataSXSD(jsonObject);
    }

    @PostMapping("/reportSXSDXlsx")
    public void reportDataSXSD(@RequestBody JSONObject jsonObject,HttpServletResponse response) {
        List<TableData> enttableData = jsonObject.getJSONArray("tableData").toJavaList(TableData.class);

        List<ExporttableData> list = new ArrayList<>();
        for (TableData ledgerPlan : enttableData) {
            ExporttableData exportLedgerPlan = new ExporttableData();
            BeanUtils.copyProperties(ledgerPlan,exportLedgerPlan);
            list.add(exportLedgerPlan);
        }
        try {
            Map<String,Object> data = new HashMap();
            data.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\酸洗生产速度统计.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
            ExcelUtilsPOI.exportExcel("酸洗生产速度统计报表",response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/reportppXlsx")
    public void reportDataPP(@RequestBody JSONObject jsonObject,HttpServletResponse response) {
        List<TableDataPP> enttableData = jsonObject.getJSONArray("tableData").toJavaList(TableDataPP.class);

        List<ExportTableDataPP> list = new ArrayList<>();
        for (TableDataPP ledgerPlan : enttableData) {
            ExportTableDataPP exportLedgerPlan = new ExportTableDataPP();
            BeanUtils.copyProperties(ledgerPlan,exportLedgerPlan);
            list.add(exportLedgerPlan);
        }
        try {
            Map<String,Object> data = new HashMap();
            data.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\连退跑偏情况总计.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
            ExcelUtilsPOI.exportExcel("连退跑偏情况总计报表",response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/reportFormsFindByDataExcel")
    public void reportFormsFindByDataExcel(@RequestBody TbDsjActWeightDto dto, HttpServletResponse response) {
        reportFormsService.reportFormsFindByDataExcel(dto, response);
    }

    @PostMapping("/findByStatistics")
    public ResultData findByStatistics(@RequestBody TbDsjActWeightDto dto) {
        return reportFormsService.findByStatistics(dto);
    }

    /**
     * 日产量报表
     *
     * @param dto
     * @return
     */
    @PostMapping("/findByDailyProduction")
    public ResultData findByDailyProduction(@RequestBody TbDsjActWeightDto dto) {
        return reportFormsService.findByDailyProduction(dto);
    }

    /**
     * 小时产量报表
     *
     * @param dto
     * @return
     */
    @PostMapping("/findByHourlyProduction")
    public ResultData findByHourlyProduction(@RequestBody TbDsjActWeightDto dto) {
        return reportFormsService.findByHourlyProduction(dto);
    }

    /**
     * 单卷生产效率分析
     *
     * @param dto
     * @return
     */
    @PostMapping("/aosrpe")
    public ResultData findAosrpe(@RequestBody AosrpeDto dto) {
        return reportFormsService.findAosrpe(dto);
    }

    /**
     * 单卷生产效率分析导出
     *
     * @param dto
     * @return
     */
    @PostMapping("/excelAosrpe")
    public void aosrpeExcel(@RequestBody AosrpeDto dto, HttpServletResponse response) {
        reportFormsService.aosrpeExcel(dto, response);
    }

    /**
     * 四班生产效率分析
     *
     * @param dto
     * @return
     */
    @PostMapping("/findProductionEfficiencyOfFourShifts")
    public ResultData findProductionEfficiencyOfFourShifts(@RequestBody AosrpeDto dto) {
        return reportFormsService.findProductionEfficiencyOfFourShifts(dto);
    }

    /**
     * 四班成材率分析
     *
     * @param dto
     * @return
     */
    @PostMapping("/yieldAnalysis")
    public ResultData yieldAnalysis(@RequestBody TbDsjActWeightDto dto) {
        return reportFormsService.yieldAnalysis(dto);
    }

    /**
     * 各产线当班产量
     *
     * @param dto
     * @return
     */
    @PostMapping("/findOnDutyProduction")
    public ResultData findOnDutyProduction(@RequestBody TbDsjActWeightDto dto) {
        return reportFormsService.findOnDutyProduction(dto);
    }
}
